package cn.tedu.egarticle0618.controller;

import cn.tedu.egarticle0618.base.response.JsonResult;
import cn.tedu.egarticle0618.base.response.StatusCode;
import cn.tedu.egarticle0618.mapper.ArticleMapper;
import cn.tedu.egarticle0618.mapper.UserMapper;
import cn.tedu.egarticle0618.pojo.dto.UserArticleParam;
import cn.tedu.egarticle0618.pojo.dto.UserLoginParam;
import cn.tedu.egarticle0618.pojo.entity.Article;
import cn.tedu.egarticle0618.pojo.vo.ArticleVO;
import cn.tedu.egarticle0618.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Tag(name = "01-用户模块")
@Slf4j
@Validated
@RestController
@RequestMapping("/v1/user/")
public class Controller {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ArticleMapper articleMapper;

    @PostMapping("login")
    @Operation(summary = "01-登录模块")
    @ApiOperationSupport(order = 10)
    public JsonResult userLogin(@RequestBody UserLoginParam userLoginParam, HttpSession session) {
        log.debug("userLoginParam:" + userLoginParam);
        UserVO userVO = userMapper.login(userLoginParam);
        if (userVO == null) {
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        session.setAttribute("user", userVO);
        return JsonResult.isCorrect(userVO);
    }

    @PostMapping("article")
    @Operation(summary = "02-发表文章模块")
    @ApiOperationSupport(order = 20)
    public JsonResult articleParam(@Validated @RequestBody UserArticleParam userArticleParam, HttpSession session) {
        log.debug("userArticleParam:" + userArticleParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        Article article = new Article();
        BeanUtils.copyProperties(userArticleParam, article);
        article.setUserId(userVO.getId());
        articleMapper.add(article);
        return JsonResult.isCorrect();
    }

    @GetMapping("selectByArticleId")
    @Operation(summary = "03-查询指定文章模块")
    @ApiOperationSupport(order = 30)
    @Parameter(name = "id", description = "文章id", required = true)
    public JsonResult select(@Range(min = 1, max = 100, message = "文章id要求在1-100之间") Long id) {
        log.debug("id:" + id);
        ArticleVO articleVO = articleMapper.query(id);
        if (articleVO == null) {
            return new JsonResult(StatusCode.OPERATION_SUCCESS);
        }
        return JsonResult.isCorrect(articleVO);
    }

    @PostMapping("upload")
    @Operation(summary = "04-文件上传模块")
    @ApiOperationSupport(order = 40)
    public JsonResult upload(MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID() + suffix;
        String dirPath = "d:/files";
        SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
        String datePath = sdf.format(new Date());
        File dirFile = new File(dirPath + datePath);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        String filePath = dirPath + datePath +fileName;
        file.transferTo(new File(filePath));
        return JsonResult.isCorrect(datePath+fileName);
    }
}
